/*
 * lib_uart.h
 *
 *  Created on: 28 июля 2010
 *      Author: ugn
 */

#ifndef LIB_UART_H_
#define LIB_UART_H_

#include "global.h"

#ifdef __cplusplus
PR_BEGIN_EXTERN_C
#endif


/* ----------------------- Type definitions ---------------------------------*/

#define				MB_ADDR	0
#define				MB_FUNC	1
#define				MB_DATA	2
#define 			MB_SIZE_MIN     4
#define 			MB_SIZE_MAX     256



typedef enum {
    MB_PAR_NONE,                /*!< No parity. */
    MB_PAR_ODD,                 /*!< Odd parity. */
    MB_PAR_EVEN                 /*!< Even parity. */
} eMBParity;

typedef enum {
    STATE_RX_INIT,              //	Receiver is in initial state.
    STATE_RX_IDLE,              //	Receiver is in idle state.
    STATE_RX_RCV,               //	Frame is beeing received.
    STATE_RX_ERROR              //	If the frame is invalid.
} eMBRcvState;

typedef enum {
    STATE_TX_IDLE,              //	Transmitter is in idle state.
    STATE_TX_XMIT               //	Transmitter is in transfer state.
} eMBSndState;


/* ----------------------- Supporting functions -----------------------------*/

//BOOL MYLIBSECTION xMBPortEventInit ( void );

//BOOL            xMBPortEventPost( eMBEventType eEvent );

//BOOL            xMBPortEventGet(  /*@out@ */ eMBEventType * eEvent );

/* ----------------------- Serial port functions ----------------------------*/

//BOOL MYLIBSECTION xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity );

//void            vMBPortClose( void );

//void            xMBPortSerialClose( void );

//void            vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable );

//BOOL            xMBPortSerialGetByte( CHAR * pucByte );

//BOOL            xMBPortSerialPutByte( CHAR ucByte );

/* ----------------------- Callback for the protocol stack ------------------*/

/*!
 * \brief Callback function for the porting layer when a new byte is
 *   available.
 *
 * Depending upon the mode this callback function is used by the RTU or
 * ASCII transmission layers. In any case a call to xMBPortSerialGetByte()
 * must immediately return a new character.
 *
 * \return <code>TRUE</code> if a event was posted to the queue because
 *   a new byte was received. The port implementation should wake up the
 *   tasks which are currently blocked on the eventqueue.
 */
//extern
//BOOL ( *pxMBFrameCBByteReceived ) ( void );
//extern
BOOL ( *pxMBFrameCBTransmitterEmpty ) ( void );
//extern
BOOL ( *pxMBPortCBTimerExpired ) ( void );

/* ----------------------- TCP port functions -------------------------------*/
//BOOL            xMBTCPPortInit( USHORT usTCPPort );

//void            vMBTCPPortClose( void );

//void            vMBTCPPortDisable( void );

//BOOL            xMBTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength );

//BOOL            xMBTCPPortSendResponse( const UCHAR *pucMBTCPFrame, USHORT usTCPLength );






/* ----------------------- Timers functions ---------------------------------*/
//BOOL MYLIBSECTION xMBPortTimersInit( USHORT usTimeOut50us );

//void            xMBPortTimersClose( void );

//void MYLIBSECTION vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable );

//void            vMBPortTimersDelay( USHORT usTimeOutMS );

//void MYLIBSECTION vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable );

//BOOL MYLIBSECTION xMBPortSerialPutByte( CHAR ucByte );

//BOOL MYLIBSECTION xMBPortSerialGetByte( CHAR * pucByte );

//BOOL MYLIBSECTION xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity );

void SerialTimer35Expired( void );
eMBErrorCode SerialSendFrame (const UCHAR SlaveAddress, const UCHAR MBFunc, const UCHAR *Frame, const USHORT FrameLength );
void SerialInit(ULONG ulBaudRate, eMBParity eParity );
void	MYLIBSECTION	SerialEnable		( BOOL xRxEnable, BOOL xTxEnable );
void	MYLIBSECTION	SerialPutByte		( u08 ucByte );


#ifdef __cplusplus
PR_END_EXTERN_C
#endif


#endif /* LIB_UART_H_ */
